<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Scheduler extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->base = $this->config->item('base_url');
		$this->resetcss = $this->config->item('reset');
		$this->css = $this->config->item('css');
		$this->load->model('db_scheduler', '', TRUE);
	}

	public function index()
	{
		if ($this->session->userdata('stat')==1) {
			$head['base'] = $this->base;
			$content['base'] = $this->base;
			$head['reset'] = $this->resetcss;
			$head['css'] = $this->css;
			if ($this->session->flashdata('schedule_status') != '') {
				$content['schedule_status'] = $this->session->flashdata('schedule_status');
			} else {
				$content['schedule_status'] = '';
			}
			$content['instances'] = '';
			$content['instances'] = $this->db_scheduler->getMySchedule();
			$this->load->view('header', $head);
			$this->load->view('nav');
			$this->load->view('my_schedule', $content);
			$this->load->view('footer');
		} else {
			$this->session->set_flashdata('login_status', 'Please login to access that page.');
			redirect('user');
		}
	}
	
	public function browse($stream = 0, $track = 0, $page = 1)
	{
		if ($this->session->userdata('stat')==1) {
			$this->session->set_userdata('browse_page', $page);
			$this->session->set_userdata('browse_stream', $stream);
			$this->session->set_userdata('browse_track', $track);
			$head['base'] = $this->base;
			$content['base'] = $this->base;
			$head['reset'] = $this->resetcss;
			$head['css'] = $this->css;
			$this->load->view('header', $head);
			$this->load->view('nav');
			if ($this->session->userdata('browse_stream') == 0) {  // if stream is 0, track must be 0
				$content['streams'] = $this->db_scheduler->getStreams();
				$content['tracks'] = '';
			} elseif ($this->session->userdata('browse_stream') > 0 && $this->session->userdata('browse_track') == 0) {
				$content['streams'] = $this->db_scheduler->getStreamName($stream);
				$content['tracks'] = $this->db_scheduler->getTracks($stream);
			} else {
				$content['streams'] = $this->db_scheduler->getStreamName($stream);
				$content['tracks'] = $this->db_scheduler->getTrackName($track);
			}
			$content['sessions'] = $this->db_scheduler->getSessions($stream, $track, $page);
			$this->load->view('list', $content);
			$this->load->view('footer');
		} else {
			$this->session->set_flashdata('login_status', 'Please login to access that page.');
			redirect('user');
		}
	}
	
	public function session($id = '', $name = '')
	{
		if ($this->session->userdata('stat')==1) {
			$head['base'] = $this->base;
			$content['base'] = $this->base;
			$head['reset'] = $this->resetcss;
			$head['css'] = $this->css;
			$this->load->view('header', $head);
			$this->load->view('nav');
			if ($id == '') {
				$content['basic'] = 'No information could be found for this session. Please try again.';
				$content['instances'] = 'No sessions can be scheduled at this time. Please try again.';
			} else {
				$content['basic'] = $this->db_scheduler->getBasic($id);
				$content['instances'] = $this->db_scheduler->getInstances($id);
			}
			$this->load->view('session_detail', $content);
			$this->load->view('footer');
		} else {
			$this->session->set_flashdata('login_status', 'Please login to access that page.');
			redirect('user');
		}
	}

	public function add($iid = '', $bypass = 0)
	{
		if ($this->session->userdata('stat')==1) {
			if ($iid == '') {
				$this->session->set_flashdata('schedule_status', 'No session was added to your schedule because you did not select one. Please try again.');
				redirect('scheduler');
			} else {
				$result = $this->db_scheduler->addSession($iid, $bypass);
				if ($result == 'no conflicts') { // no conflicts were found
					$this->session->set_flashdata('schedule_status', 'The session has successfully been added to your schedule below.');
					redirect('scheduler');
				} else { // there was a conflict
					$link_add = $this->base . 'scheduler/add/' . $iid . '/1';
					$link_browse =  $this->base . 'scheduler/browse/' . $this->session->userdata('browse_stream') . '/' . $this->session->userdata('browse_track') . '/' .$this->session->userdata('browse_page');
					$this->session->set_flashdata('action', 'Schedule');
					$this->session->set_flashdata('link_add', $link_add);
					$this->session->set_flashdata('link_browse', $link_browse);
					$this->session->set_flashdata('conflict_html', $result);
					redirect('scheduler/oops');
				}
			}
		} else {
			$this->session->set_flashdata('login_status', 'Please login to access that page.');
			redirect('user');
		}
	}
	
	public function reschedule($iid = '', $iid2 = '', $bypass = 0)
	{
		if ($this->session->userdata('stat')==1) {
			if ($iid == '') {
				$this->session->set_flashdata('schedule_status', 'No session was added to your schedule because you did not select one. Please try again.');
				redirect('scheduler');
			} else {
				$result = $this->db_scheduler->addSession($iid, $bypass);
				if ($result == 'no conflicts') { // no conflicts were found
					$result = $this->db_scheduler->removeSession($iid2);
					$this->session->set_flashdata('schedule_status', 'The conflicting session has been rescheduled. You may now try to schedule your desired session again.');
					redirect('scheduler');
				} else { // there was a conflict
					$link_add = $this->base . 'scheduler/reschedule/' . $iid . '/' . $iid2 . '/1';
					$link_browse =  $this->base . 'scheduler/browse/' . $this->session->userdata('browse_stream') . '/' . $this->session->userdata('browse_track') . '/' .$this->session->userdata('browse_page');
					$this->session->set_flashdata('action', 'Reschedule');
					$this->session->set_flashdata('link_add', $link_add);
					$this->session->set_flashdata('link_browse', $link_browse);
					$this->session->set_flashdata('conflict_html', $result);
					redirect('scheduler/oops');
				}
			}
		} else {
			$this->session->set_flashdata('login_status', 'Please login to access that page.');
			redirect('user');
		}
	}
	
	public function oops()
	{
		// function to drive page to display conflicts if conflicts are found
		if ($this->session->userdata('stat')==1) {
			$head['base'] = $this->base;
			$content['base'] = $this->base;
			$head['reset'] = $this->resetcss;
			$head['css'] = $this->css;
			$content['action'] = $this->session->flashdata('action');
			$content['link_add'] = $this->session->flashdata('link_add');
			$content['link_browse'] = $this->session->flashdata('link_browse');
			$content['conflict_html'] = $this->session->flashdata('conflict_html');
			$content['instances'] = '';
			$this->load->view('header', $head);
			$this->load->view('nav');
			$this->load->view('oops', $content);
			$this->load->view('footer');
		} else {
			$this->session->set_flashdata('login_status', 'Please login to access that page.');
			redirect('user');
		}
		
	}
	
	public function remove($iid = '')
	{
		if ($this->session->userdata('stat')==1) {
			if ($iid == '') {
				$this->session->set_flashdata('schedule_status', 'The selected session could not be removed from your schedule. Please try again.');
				redirect('scheduler');
			} else {
				$result = $this->db_scheduler->removeSession($iid);
				$this->session->set_flashdata('schedule_status', 'The session has successfully been removed from your schedule below.');
				redirect('scheduler');
			}
		} else {
			$this->session->set_flashdata('login_status', 'Please login to access that page.');
			redirect('user');
		}
	}

}

/* End of file scheduler.php */
/* Location: ./roadrunner/controllers/scheduler.php */